package com.unit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

  public class Json {


	public static Map<String, Object> getMap(String jsonString) 
	{
		JSONObject jsonObject; 
		try 
		{ 
			jsonObject = new JSONObject(jsonString);   @SuppressWarnings("unchecked") 
			Iterator<String> keyIter = jsonObject.keys(); 
			String key; 
			Object value; 
			Map<String, Object> valueMap = new HashMap<String, Object>(); 
			while (keyIter.hasNext()) 
			{
				key = (String) keyIter.next(); 
				value =jsonObject.get(key); 
				valueMap.put(key, value);
				Log.d(key,value.toString());
			} 
			return valueMap; 
      }
		catch (JSONException e) 
		{
			e.printStackTrace();
			} 
		return null; 
    } 
	public static List<Map<String, Object>> getList(String jsonString)
	{
		List<Map<String, Object>> list = null; 
		try 
		{ 
			JSONArray jsonArray = new JSONArray(jsonString); 
			JSONObject jsonObject; 
			list = new ArrayList<Map<String, Object>>(); 
			for (int i = 0; i < jsonArray.length(); i++) 
			{ 
				jsonObject = jsonArray.getJSONObject(i); 
				list.add(getMap(jsonObject.toString())); 
	       } 
			} 
		catch (Exception e) 
		{ 
			e.printStackTrace(); 
			} 
		return list; 
		}
	public static String JsontoString(List<?> list) throws Exception {
		JSONArray array = new JSONArray();
		JSONObject member = null;
		for (Object obj : list) {
			member = new JSONObject();
			member.put("site", ((Map<?, ?>) obj).get("site"));
			member.put("studenturl", ((Map<?, ?>) obj).get("studenturl"));
			member.put("teacherurl", ((Map<?, ?>) obj).get("teacherurl"));
			array.put(member);
		}
		return array.toString();
	}

}
